﻿using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using NLog;
using System.Threading;
using System.Diagnostics;
using MySql.Data.MySqlClient;
using DevLib.Data;
using System.Configuration;
using System.Globalization;

namespace sqltomongo
{
    class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        /// <summary>
        /// 单次同步数量
        /// </summary>
        private static int PerCount { get; } = int.Parse(ConfigurationManager.AppSettings["per_count"].ToString());
        
        static  void Main(string[] args)
        {
            try
            {
                string optionA = "【a】同步所有数据", optionB = "【b】同步时间区间内数据";
                Console.WriteLine("\r\n");
                Console.WriteLine("===============================================================================");
                Console.WriteLine("==========================Sqlserver=>MongoDB数据同步程序========================");
                Console.WriteLine("===============================================================================");
                Console.WriteLine($"\r\n{optionA}\n\n{optionB}\r\n");
                Console.WriteLine("请选择操作类型：");
                lbl:
                string seletedOption = Console.ReadLine();
                if (seletedOption != "a" && seletedOption != "b") {
                    Console.WriteLine("！！！输入错误请重新输入\r\n");
                    goto lbl;
                }
                string selectOptionText = seletedOption == "a" ? optionA : (seletedOption == "b" ? optionB : "");
                Console.WriteLine($"您选择的是:{selectOptionText}，确认执行（Y）,按任意键取消");
                if (Console.ReadLine().Trim().ToLower() == "y")
                {
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    if (seletedOption == "b")
                    {
                        ParamllelTask.UpSertData();
                        watch.Stop();
                        ParamllelTask.WriteLog(string.Format("----同步完成,耗时{0}秒", watch.ElapsedMilliseconds / 1000));
                        Console.Read();
                    }
                    else if (seletedOption == "a")
                    {
                        Console.WriteLine("----开始执行sqlserver-->mongodb数据同步----\r\n\r\n");
                        logger.Info("----开始执行同步----");
                        if (ConfigurationManager.AppSettings["multi_thread"].ToString().ToLower() == "true")
                        {
                            //ParamllelTask.StartParellelThread(PerCount);
                        }
                        else
                        {
                            Channels.ForEach(channel => ParamllelTask.Start(PerCount, channel));
                        }
                        watch.Stop();
                        ParamllelTask.WriteLog(string.Format("----同步完成,耗时{0}秒", watch.ElapsedMilliseconds/1000));
                    }
                }
                else
                {
                    Console.WriteLine("  输入错误，请重新输入！\r\n");
                    goto lbl;
                }
            }
            catch (Exception ee)
            {
                logger.Error($"----同步异常-----,原因->{ee.Message}----");
            }
        }
        /// <summary>
        /// 渠道列表
        /// </summary>
        private static List<string> Channels
        {
            get
            {
                List<string> listChannel = new List<string>();
                for (int i = 0; i <= 38; i++)
                {
                    if (i >= 36 && i <= 37){  continue;}
                    listChannel.Add(i.ToString());
                }
                return listChannel;
            }
        }

    }
}
